
<?php
class It_Model_Resource extends Zend_Db_Table_Abstract
{
    protected $_name = 'resources';
    protected $_primary = 'id';
    protected  $_order = 'id';

    public function getAllData(){
        $query = $this->select()
                    ->from($this->_name, array('id','resourceName'));
        
        return $this->fetchAll($query);
    }
    /*******INSERT*******/
    public function insertItem($info)
    {
        $this->insert($info);
        return $this->_identity;
    }

    /*******UPDATE*******/
    public function updateItem($id,$info)
    {
        $this->update( $info, "id=" . (int) $id );
    }

    /*******DELETE*******/
    public function deleteItem($id)
    {
        $this->delete( "id=" . (int) $id );
    }

    /*******GET ITEMS*******/
    public function getItems($option = array())
    {
        $count = isset($option['count'])? $option['count'] : 0;
        $offset = isset($option['offset'])? $option['offset'] : 0;
        $order = isset($option['order'])? $option['order'] : $this->_order;
        $sort = isset($option['sort'])? $option['sort'] : 'asc';
        $select = $this->select()->from(array('s' => "resources" ),
                        array('id',
                            'resourceName',
                            'moduleName',
                            'controllerName',
                            'actionName',
                            'isDetail'))
                ->order("{$order} {$sort}")
                ->limit($count,$offset);
        //
        if(isset($option['moduleName'])){
            $select->where('moduleName', $option['moduleName']);
        }

        $results = $this->fetchAll($select);
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }

    /*******GET ITEM*******/
    public function getItem($id)
    {
        $select = $this->select()->from(array('s' => "resources" ),
                        array(
                            'id',
                            'moduleName',
                            'controllerName',
                            'actionName'
                        ))
                ->where("id = {$id}");
        $result = $this->fetchRow($select);
        if ( !$result )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $result;
    }

    /*******COUNT ITEM*******/
    public function countItems()
    {
        $query = $this->select()
                ->from(resources,'count(*) as total');
        $result = $this->fetchRow($query);
        return $result->total;
    }

    public function getAllModules()
    {
        $smtp = $this->select()->from('resources',array('id','moduleName'))->group('moduleName');
        return $this->fetchAll($smtp);
    }
}